package converter;

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
import javax.faces.convert.FacesConverter;

import model.bean.Nucleo;
import model.dao.NucleoDao;

@FacesConverter(value = "NucleoConverter")
public class NucleoConverter implements Converter {

	@Override
	public Object getAsObject(FacesContext arg0, UIComponent arg1, String arg2) {
		if (!arg2.trim().isEmpty()) {
			NucleoDao dao = new NucleoDao();
			if (!arg2.contains("Selecione...")) {
				Nucleo nucleo = dao.find(Integer.parseInt(arg2));
				return nucleo;
			}
		}
		return null;
	}

	@Override
	public String getAsString(FacesContext context, UIComponent component,
			Object object) {
		if (object != null && object instanceof Nucleo) {
			Nucleo nucleo = (Nucleo) object;
			return nucleo.getIdNucleo().toString();
		}
		return null;
	}

}
